<%@ Page Language="C#" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
    public const int CURRENT_MODULE = 51;
    
    int rowCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        
        if (Page.IsPostBack)
        {
            string strSearch, strSector, strCountry;
            
            strSearch = txtSearch.Text;
            strSector = Request.Form["cboSector"];
            strCountry = cboCountry.SelectedValue;

            bindResult(strSearch, strSector, strCountry);
        }
        else
        {
            loadData();
        }
    }

    void loadData()
    {
        ListItem li = new ListItem("*-ALL-*", "-1");
        List.fillList(cboCountry, "sp_Select_cms_Lookup 'country_region'");

        cboCountry.Items.Insert(0, li);
    }

    void bindResult(string search, string sector, string country)
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using(SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_search_client", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@search", SqlDbType.NVarChar, 50).Value = search;
            cmd.Parameters.Add("@sector", SqlDbType.Int).Value = sector;
            cmd.Parameters.Add("@country", SqlDbType.Int).Value = country;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            rptResult.DataSource = dr;
            rptResult.DataBind();
            // record count
            rowCount = rptResult.Items.Count;

            if (rowCount == 0)
            {
                NoMatch.Visible = true;
            }
            else
            {
                NoMatch.Visible = false;
            }
        }
    }

    string renderItem(RepeaterItem item)
    {
        string strHTML = "";
        int i = item.ItemIndex;
        int id = Convert.ToInt32(DataBinder.Eval(item.DataItem, "Company_ID"));
        string name = DataBinder.Eval(item.DataItem, "Company_Name").ToString();
        string country = DataBinder.Eval(item.DataItem, "Country").ToString();
        string phone = DataBinder.Eval(item.DataItem, "Phone_Number").ToString();
        if ((i % 2) == 0)
        {
            strHTML = "<tr bgcolor='#efefef'><td>" + (i + 1) + "</td><td><b><a href=\"javascript:selectClient('" + id + "')\">" + name + "</a></b></td><td>" + country + "</td><td>" + phone + "</td></tr>";
        }
        else
        {
            strHTML = "<tr><td>" + (i + 1) + "</td><td><b><a href=\"javascript:selectClient('" + id + "')\">" + name + "</a></b></td><td>" + country + "</td><td>" + phone + "</td></tr>";
        }
        return strHTML;
    }
     

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Search Client</title>
    <link href="css/style_1.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        body {margin:0}
    </style>
    <script language="javascript">
        function selectClient(id)
        {
            var cbo = eval("window.opener.document." + window.opener.obj_target);
            if(id!="")
            {
                if(cbo)
                {                    
                    for(i=0;i<cbo.length;i++)
                    {                        
                        if(id==cbo[i].value)
                        {
                            cbo.selectedIndex = i;
                            // window.alert(id);
                            break;
                        }
                    }
                    window.close();
                    cbo.focus();
                }
            }
        }
    </script>
</head>
<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="348" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">
						&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">
						&nbsp;</td>
				</tr>
				
				
			</table>
			
			<!-- body -->
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" valign="top" >
						<form id="frmSearch" runat="server">
                            <hr size="1" color="#10147e" width="100%" noshade />
                            <table border="0" cellpadding="3" align="center" style="font-size: 7.5pt; font-weight: bold;">
                                <tr>
                                    <td>
                                        Search :
                                    </td>
                                    <td>
                                        Sector :
                                    </td>
                                    <td>
                                        Country :
                                    </td>
                                    <td>
                                        &nbsp;</td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:TextBox ID="txtSearch" runat="server" /></td>
                                    <td>
                                        <!--#include file="includes/inc_sectors.aspx"-->
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="cboCountry" runat="server" /></td>
                                    <td>
                                        <asp:Button ID="cmdSearch" Text="Search" runat="server" /></td>
                                </tr>
                            </table>
                            <hr size="1" color="#10147e" width="100%" noshade />
                            <div style="height: 150px; overflow: auto; vertical-align: top;">
                                <asp:Repeater ID="rptResult" runat="server">
                                    <HeaderTemplate>
                                        <table width="510" align="center" border="0" class="data" cellspacing="1" cellpadding="5">
                                            <tr class="header">
                                                <td width="30">
                                                    No</td>
                                                <td width="250">
                                                    Client name</td>
                                                <td width="80">
                                                    Country</td>
                                                <td width="150">
                                                    Phone number</td>
                                            </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <%# renderItem((RepeaterItem)Container) %>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <tr>
                                            <td class="orange" colspan="4" align="center">
                                                <!--<b><%//=rowCount %></b> record(s) found.-->
                                            </td>
                                        </tr>
                                        </table>
                                    </FooterTemplate>
                                </asp:Repeater>
                                <div align="center" class="orange">
                                    <asp:Literal Visible="false" ID="NoMatch" Text="There is no matched records." runat="server" />
                                </div>
                            </div>
                        </form>
					</td>
				</tr>
			</table>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>

    
</body>
</html>
